layui.use(['form', 'table', 'laydate'], function () {
    var form = layui.form,
        layer = layui.layer,
        table = layui.table,
        $ = layui.$,
        laydate = layui.laydate;

    /**
     * 初始化表单，要加上，不然刷新部分组件可能会不加载
     */
    form.render();

    // 当前弹出层，防止ID被覆盖
    var parentIndex = layer.index;

    //初始化
    $(function(){
        var id = $("#id").val();
        if(id != '' && id != null && id != undefined){
            var index = layer.load(2);
            var url = "/web/boBookDirectory/getInfoById";
            $.get(url, {id: id}, function(data){
                layer.close(index);
                if(data.state == "200"){
                    $("#bookName").val(data.bookName);
                    $("#bookId").val(data.bookId);
                    $('#directoryName').val(data.directoryName);
                    $("#content").val(data.content);
                }
                form.render();
            })
        }
    });

    //监听提交
    form.on('submit(saveBtn)', function (data) {
        var index = layer.load(2);
        var url = "/web/boBookDirectory/save";
        $.post(url, data.field, function(res){
            if(res.state == "200"){
                layer.msg(res.message, {icon:1});
            }else{
                layer.msg(res.message, {icon:2});
            }
            // 关闭弹出层
            layer.close(index);
            layer.close(parentIndex);
        }, 'json');

        return false;
    });

    //点击选择书籍按钮
    $("#BookBtn").on("click", function(){
        var bookId = $("#bookId").val();
        var bookName = $("#bookName").val();
        var content = layuimini.getHrefContent('/web/boBookDirectory/chooseBook?bookId=' + bookId + "&bookName=" + bookName);
        var index = layer.open({
            title: '选择书籍',
            type: 1,
            shade: 0.2,
            maxmin:true,
            shadeClose: false,
            area: ['80%','80%'], //宽高
            content: content
        });
        // $(window).on("resize", function () {
        //     layer.full(index);
        // });

        return true;
    });

    $("#closeBtn").on("click", function(){
        // 关闭弹出层
        layer.close(parentIndex);
    });

});